package com.eqdd.yiqidian.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.library.percentUtil.WindowUtil;

/**
 * Created by lvzhihao on 17-5-16.
 */

public class NewPinItemDecoration extends RecyclerView.ItemDecoration {
    private static final String TAG = "PinItemDecoration";
    private int textLeft;


    private DecorationCallback mDecorationCallback;

    private int topGap;
    private int from;
    private int left;
    private int right;


    public NewPinItemDecoration(Context context, DecorationCallback mDecorationCallback, int from) {
        init(mDecorationCallback, from);
    }

    private void init(DecorationCallback mDecorationCallback, int from) {
        this.from = from;
        this.mDecorationCallback = mDecorationCallback;

        //决定悬浮栏的高度等
        topGap = WindowUtil.width / 10;
        textLeft = WindowUtil.width / 10;
    }

    public NewPinItemDecoration(Context context, DecorationCallback mDecorationCallback) {
        init(mDecorationCallback, 0);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int pos = parent.getChildAdapterPosition(view);
        //只有是同一组的第一个才显示悬浮栏
        if (isFirstInGroup(pos)) {
            outRect.top = topGap;
        } else {
            outRect.top = 0;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(view);
            if (isFirstInGroup(position)) {
                float top = view.getTop() - topGap;
                View groupView = mDecorationCallback.getGroupView(position);
                Bitmap drawingCache = getBitmap(left, right, groupView);
                c.drawBitmap(drawingCache, left, top, new Paint());
            }
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        if (parent.getChildCount() > 0) {//有内容
            //获取屏幕上第一个item在recyclerView中所占的位置
            int firstPosition = parent.getChildAdapterPosition(parent.getChildAt(0));
            if (firstPosition - from >= 0) {//在可绘制区域
                int childCount = parent.getChildCount();//屏幕上占的数量
                int top = topGap;
                for (int i = 0; i < childCount - 1; i++) {//获取可绘制区域
                    View view = parent.getChildAt(i);
                    int position = parent.getChildAdapterPosition(view);
                    if (parent.getAdapter().getItemCount() - 1 == position) {
                        return;
                    }
                    String groupLabelNow = mDecorationCallback.getGroupLabel(position);
                    String groupLabelNext = mDecorationCallback.getGroupLabel(position + 1);
                    if (TextUtils.isEmpty(groupLabelNow) || TextUtils.isEmpty(groupLabelNext)) {
                        return;
                    }
                    if (!groupLabelNow.equals(groupLabelNext)) {
                        View nextView = parent.getChildAt(i + 1);
                        if (nextView.getTop() < (topGap * 2)) {
                            top = nextView.getTop() - topGap;
                            break;
                        }
                    }
                }
                View groupView = mDecorationCallback.getGroupView(firstPosition);
                Bitmap drawingCache = getBitmap(left, right, groupView);
                c.drawBitmap(drawingCache, left, top - topGap, new Paint());
                return;
            }
        }
    }

    private Bitmap getBitmap(int left, int right, View groupView) {
        groupView.setDrawingCacheEnabled(true);
        groupView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        groupView.layout(0, 0, right - left, topGap);
        groupView.buildDrawingCache();
        Bitmap drawingCache = groupView.getDrawingCache();
        drawingCache = Bitmap.createBitmap(drawingCache);
        groupView.setDrawingCacheEnabled(false);
        return drawingCache;
    }

    private boolean isFirstInGroup(int pos) {
        if (pos < from) {
            return false;
        }
        if (pos == from && !TextUtils.isEmpty(mDecorationCallback.getGroupLabel(pos))) {
            return true;
        }

        String prevLabel = mDecorationCallback.getGroupLabel(pos - 1);
        String label = mDecorationCallback.getGroupLabel(pos);
        if (TextUtils.isEmpty(prevLabel) || TextUtils.isEmpty(label)) {
            return false;
        }
        return !prevLabel.equals(label);

    }

    public interface DecorationCallback {
        String getGroupLabel(int position);

        View getGroupView(int position);
    }
}  